
//
//  CXEmotion.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/28.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXEmotion: NSObject, NSCoding {
    /// 描述
    var chs: String?
    /// 表情文件名
    var png: String?
    /// 0 非emoji 1 emoji
    var type: String? {
        
        didSet {
            
            isEmoji = (type == "1")
        }
    }
    /// emoji的编码
    var code: String?
    //自定义属性
    /// 默认不是emoji
    var isEmoji: Bool = false
    /// 路径
    var path: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value:AnyObject?, forUndefinedKey key: String) {
        //do nothing
    }
    ///  重写比较对象是否相等的方法
    override func isEqual(object: AnyObject?) -> Bool {
        if let emotion = object as? CXEmotion {
            
            if self.type == emotion.type {
                
                //类型相同 判断是不是Emoji
                if self.isEmoji {
                    if self.code == emotion.code {
                        return true
                    }
                } else {
                    if self.chs == emotion.chs {
                        return true
                    }
                }
            }
        }
        //不符合条件的 交给父类去处理
        return super.isEqual(object)
        
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(path, forKey: "path")
        aCoder.encodeObject(type, forKey: "type")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeBool(isEmoji, forKey: "isEmoji")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        chs = aDecoder.decodeObjectForKey("chs") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
        path = aDecoder.decodeObjectForKey("path") as? String
        type = aDecoder.decodeObjectForKey("type") as? String
        code = aDecoder.decodeObjectForKey("code") as? String
        isEmoji = aDecoder.decodeBoolForKey("isEmoji")
    }
}
